package edu.uwm.RobberKnight.View;

import javax.swing.JPanel;
import javax.swing.JLabel;

import edu.uwm.RobberKnight.Controller.RobberKnightsController;
import edu.uwm.RobberKnight.Model.GameState;
import edu.uwm.RobberKnight.Model.Player;

public class InstructionView extends JPanel {

	private static final long serialVersionUID = 517049873032788300L;

	private RobberKnightsController _controller;
	private JLabel lblCurplayer;
	private JLabel lblKnightsleft;
	private JLabel lblMoves;
	private JLabel lblInst;
	private Player _player;
	
	public InstructionView(final RobberKnightsController controller) {
		_controller = controller;
		_player = _controller.getCurrentPlayer();
		setLayout(null);
		
		JLabel lblCurrentPlayer = new JLabel("Current Player:");
		lblCurrentPlayer.setBounds(10, 6, 74, 14);
		add(lblCurrentPlayer);
		
		JLabel lblMovesRemaining = new JLabel("Moves Remaining:");
		lblMovesRemaining.setBounds(200, 6, 89, 14);
		add(lblMovesRemaining);
		
		JLabel lblKnightsRemaining = new JLabel("Knights Remaining:");
		lblKnightsRemaining.setBounds(333, 6, 91, 14);
		add(lblKnightsRemaining);
		
		if(_controller.getCurrentPlayer() == null) {
			lblCurplayer = new JLabel("");
			lblCurplayer.setBounds(86, 6, 46, 14);
			add(lblCurplayer);
			
			lblKnightsleft = new JLabel("");
			lblKnightsleft.setBounds(428, 6, 22, 14);
			add(lblKnightsleft);
			
			lblMoves = new JLabel("");
			lblMoves.setBounds(292, 6, 31, 14);
			add(lblMoves);
			
			lblInst = new JLabel("");
			lblInst.setBounds(459, 6, 192, 14);
			add(lblInst);
		}
		else {
			lblCurplayer = new JLabel(_player.getName());
			lblCurplayer.setBounds(86, 6, 46, 14);
			add(lblCurplayer);
			
			lblKnightsleft = new JLabel(Integer.toString(_controller.getBoard().getPlayableKnights()));
			lblKnightsleft.setBounds(428, 6, 22, 14);
			add(lblKnightsleft);
			
			lblMoves = new JLabel(Integer.toString(_controller.getBoard().getPlayableTiles()));
			lblMoves.setBounds(292, 6, 31, 14);
			add(lblMoves);
		}
		
	}
	
	public void refresh() {
		_player = _controller.getCurrentPlayer();
		if(_player != null) {
			lblCurplayer.setText(_player.getName());
			if(_controller.getBoard() != null) {
				lblKnightsleft.setText(Integer.toString(_controller.getBoard().getPlayableKnights()));
				lblMoves.setText(Integer.toString(_controller.getBoard().getPlayableTiles()));
				if(_controller.getBoard().isKnightsPlayable()) {
					if(_controller.getBoard().getPlayableTiles() > 0)
						lblInst.setText("Place knights, another tile, or end turn");
					else
						lblInst.setText("Place knights or end turn");
				}
				else if(_controller.getBoard().getPlayableTiles() > 0) {
					if(_controller.getBoard().getPlayableTiles() != 3 && _controller.getState().equals(GameState.regularPlay))
						lblInst.setText("Place a tile or end turn");
					else
						lblInst.setText("Place a tile");
				}
				else
					lblInst.setText("Turn complete");
			}
		}
	}
}
